import { message } from "antd"
import {
  getBannerImg,
  addBannerImg,
  deleteBannerImg,
}  from '../services/banner';


export default {
  namespace: 'banner',

  state: {
    fileImg:[]
  },

  effects: {
    *getBannerImg({payload}, { call, put }) {
      const data = yield call(getBannerImg);
      if(data.status == 0){
        yield put({
          type: 'updateState',
          payload: {
            fileImg:data.result
          }
        })
      }else{
       message.error(`${data.message}`)
      }
    },

    *addBannerImg({payload}, { call, put }) {
      const data = yield call(addBannerImg,payload);
      if(data.status == 0){
        yield put({
          type: 'getBannerImg',
        })
      }else{
       message.error(`${data.message}`)
      }
    },

    *deleteBannerImg({payload}, { call, put }) {
      const data = yield call(deleteBannerImg,payload);
      if(data.status == 0){
        message.success(`${data.message}`)
        yield put({
          type: 'getBannerImg',
        })
      }else{
       message.error(`${data.message}`)
      }
    },


    // *updateRechargeConfig({payload}, { call, put }) {
    //   const data = yield call(updateRechargeConfig,payload);
    //   if(data.status == 0){
    //     message.success("编辑成功!")
    //      yield put({type:"getRechargeConfig"})
    //   }else{
    //     message.error(`${data.message}`)
    //   }
    // },




  },

  reducers: {
    updateState (state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === "/banner") {
          dispatch({ type: "getBannerImg" });
        }
      });
    }
  },

};
